//
//  ZallImageDetailTextCell.m
//  ZJS-DRIVER
//
//  Created by so on 16/6/3.
//  Copyright © 2016年 liulei. All rights reserved.
//

#import "ZallImageDetailTextCell.h"

@implementation ZallImageDetailTextCell
@synthesize textHeight = _textHeight;
@synthesize detailTextLabel = _detailTextLabel;

#pragma mark - life cycle
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if(self) {
        _autoFitTextHeight = YES;
        _textHeight = 0;
    }
    return (self);
}
#pragma mark -

#pragma mark - override
- (void)layoutSubviews {
    [super layoutSubviews];
    CGRect insetFrame = UIEdgeInsetsInsetRect(self.contentView.bounds, _contentInsets);
    if(_imageView) {
        if(_autoFitImageSize) {
            _imageSize = CGSizeMake(CGRectGetHeight(insetFrame), CGRectGetHeight(insetFrame));
        }
        self.imageView.frame = CGRectMake(_contentInsets.left, _contentInsets.top, _imageSize.width, _imageSize.height);
        insetFrame.origin = CGPointMake(self.imageView.right + _gap.width, self.imageView.top);
        insetFrame.size = CGSizeMake(CGRectGetWidth(insetFrame) - self.imageView.width - _gap.width, CGRectGetHeight(insetFrame));
    }
    if(_textLabel) {
        if(_autoFitTextHeight && _detailTextLabel) {
            _textHeight = self.textLabel.font.lineHeight;
        } else {
            _textHeight = CGRectGetHeight(insetFrame);
        }
        self.textLabel.frame = CGRectMake(CGRectGetMinX(insetFrame), CGRectGetMinY(insetFrame), CGRectGetWidth(insetFrame), _textHeight);
        insetFrame.origin = CGPointMake(self.textLabel.left, self.textLabel.bottom + _gap.height);
        insetFrame.size = CGSizeMake(self.textLabel.width, CGRectGetHeight(insetFrame) - self.textLabel.height - _gap.height);
    }
    if(_detailTextLabel) {
        _detailTextLabel.frame = insetFrame;
    }
}

- (void)prepareForReuse {
    [super prepareForReuse];
    _autoFitTextHeight = YES;
    if(_textLabel) {
        _textLabel.numberOfLines = 1;
    }
    if(_detailTextLabel) {
        _detailTextLabel.text = nil;
    }
}
#pragma mark -

#pragma mark - setter
- (void)setTextHeight:(CGFloat)textHeight {
    [self willChangeValueForKey:@"textHeight"];
    _textHeight = textHeight;
    [self didChangeValueForKey:@"textHeight"];
    
    if(_textLabel) {
        _textLabel.numberOfLines = (_textHeight < 2 * _textLabel.font.lineHeight) ? 1 : 0;
    }
    
    [self willChangeValueForKey:@"autoFitTextHeight"];
    _autoFitTextHeight = NO;
    [self didChangeValueForKey:@"autoFitTextHeight"];
    
    [self setNeedsLayout];
}
#pragma mark -

#pragma mark - getter
- (UILabel *)textLabel {
    if(!_textLabel) {
        _textLabel = [[UILabel alloc] init];
        _textLabel.backgroundColor = [UIColor clearColor];
        _textLabel.font = [UIFont systemFontOfSize:16];
        _textLabel.numberOfLines = 1;
        [self.contentView addSubview:_textLabel];
        [self setNeedsLayout];
    }
    return (_textLabel);
}

- (UILabel *)detailTextLabel {
    if(!_detailTextLabel) {
        _detailTextLabel = [[UILabel alloc] init];
        _detailTextLabel.backgroundColor = [UIColor clearColor];
        _detailTextLabel.font = [UIFont systemFontOfSize:14];
        _detailTextLabel.numberOfLines = 0;
        [self.contentView addSubview:_detailTextLabel];
        [self setNeedsLayout];
    }
    return (_detailTextLabel);
}
#pragma mark -

@end
